package com.zg.common.core;

import java.io.Serializable;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.zg.common.core.exception.E;
import com.zg.common.core.exception.IException;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * 返回值定义
 * @author Zxb
 */
@SuppressWarnings("AlibabaClassNamingShouldBeCamel")
@Data
@ApiModel("返回值")
@JsonIgnoreProperties(ignoreUnknown = true)
public class R<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    private static final String MSG_SUCCESS = "操作成功";
    private static final String MSG_ERROR = "操作失败";

    @ApiModelProperty("编码")
    private Integer code;
    @ApiModelProperty("消息")
    private String message;
    @ApiModelProperty("数据")
    private T data;

    public static <T> R<T> create(Integer code, String msg, T data) {
        R<T> r = new R<>();
        r.setCode(code);
        r.setMessage(msg);
        r.setData(data);
        return r;
    }

    public static <T> R<T> success() {
        return R.create(1, MSG_SUCCESS, null);
    }

    public static <T> R<T> success(T data) {
        return R.create(1, MSG_SUCCESS, data);
    }

    public static <T> R<T> error() {
        return R.create(-1, MSG_ERROR, null);
    }

    public static <T> R<T> error(String msg) {
        return R.create(-1, msg, null);
    }

    public static <T> R<T> error(Integer code, String msg) {
        return R.create(code, msg, null);
    }

    public static <T> R<T> error(IException e) {
        return R.create(e.getCode(), e.getMsg(), null);
    }

    public Boolean isOk(){
        return this.code.equals(1);
    }
}
